<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="72px">
        <el-form-item label="物料号">
          <el-input
            v-model="searchParam.materialCode"
            placeholder="请输入物料名称或编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="耗材类型">
          <el-input
            v-model="searchParam.materialGroupName"
            placeholder="请输入耗材类型名称或编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="设备编码">
          <el-input
            v-model="searchParam.equipCode"
            placeholder="请输入设备编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button
          type="primary"
          @click="
            useDownload(
              '/pqd-etl/equfaultinfo/export',
              '耗材台账信息',
              searchParam
            )
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        :cell-style="{ 'text-align': 'center' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="expand">
          <template #default="props">
            <div ml="47px" mr="-1px">
              <el-table
                :data="props.row.nodes"
                :border="true"
                style="width: 990px"
                max-height="500"
                :cell-style="{ 'text-align': 'center' }"
              >
                <el-table-column
                  label="耗材编码"
                  prop="consumeCode"
                  width="190"
                  show-overflow-tooltip
                />
                <!-- 耗材状态;1.待使用；2.使用中；3.待判定；4；待返修；5.报废； -->
                <el-table-column
                  label="耗材状态"
                  prop="status"
                  width="170"
                  show-overflow-tooltip
                >
                  <template #default="scope">
                    <span>
                      {{ useDictionary("csmStatus", scope.row?.status) }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column
                  label="使用设备"
                  prop="equipCode"
                  width="170"
                  show-overflow-tooltip
                />
                <el-table-column
                  label="位置"
                  prop="equipPosition"
                  width="170"
                  show-overflow-tooltip
                />
                <el-table-column
                  label="使用寿命"
                  prop="workLife"
                  width="170"
                  show-overflow-tooltip
                />

                <el-table-column
                  label="使用次数"
                  prop="workCount"
                  show-overflow-tooltip
                />
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="耗材编码"
          prop="consumeCode"
          width="190"
          show-overflow-tooltip
        />
        <el-table-column
          label="耗材物料号"
          prop="materialCode"
          width="170"
          show-overflow-tooltip
        />
        <el-table-column
          label="耗材名称"
          prop="materialName"
          width="170"
          show-overflow-tooltip
        />
        <el-table-column
          label="耗材类型编码"
          prop="materialGroupCode"
          width="230"
          show-overflow-tooltip
        />
        <el-table-column
          label="耗材类型名称"
          prop="materialGroupName"
          width="230"
          show-overflow-tooltip
        />
        <el-table-column
          label="批次信息"
          prop="batchInfo"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="批次数量"
          prop="batchCount"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="耗材状态"
          prop="status"
          width="150"
          show-overflow-tooltip
        >
          <template #default="scope">
            <span>
              {{ useDictionary("csmStatus", scope.row?.status) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="使用设备"
          prop="equipCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="位置"
          prop="equipPosition"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="使用寿命"
          prop="workLife"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="使用次数"
          prop="workCount"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看生产记录明细
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <csmledgermanage-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import router from "@/router";

defineOptions({
  name: "EAMCSMCSMLEDGERMANAGE",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/eam/csmledgermanage/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  materialCode: "",
  materialGroupName: "", // 耗材类型
  equipCode: "", // 型号
};

const deleteParam = {
  api: "/eam/csmledgermanage/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/eam/csmledgermanage/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开消耗品台账管理表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  // addDialog.title = "消耗品台账管理详情";
  // getInfo(roleObj.enabled);
  // addDialog.readvisible = true;
  router.push(`/eam/csm/csmproductrecord?consumeCode=${roleObj.consumeCode}`);
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

// 耗材类型获取
const haocaiTypeList = ref(<any>[]);
const gethaocaiTypeList = async () => {
  await request({
    url: `mdm/mdmprocess/getList`,
    method: "post",
    data: {},
  }).then((res) => {
    haocaiTypeList.value = res.data;
    // console.log("haocaiTypeList.value", haocaiTypeList.value);
  });
};
onMounted(() => {
  gethaocaiTypeList();
});
</script>
